from database.db_handler import MysqlHander
from common.my_http import MyHttp
import urllib.request
from bs4 import BeautifulSoup
import glob
import jieba
import re
import threading
import hashlib
import time

'''
地震数据
'''
headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36'
}

class DizhenData:
    def __init__(self):
        self.db = MysqlHander("config.ini")
        
    def insert_data(self, date, title, type_id, url, site):
        sql = "insert into market_data (Fdate, Ftype, Ftitle, Furl, Fsite, Fcreate_time, Fmodify_time) values "
        sql += "('" + date + "'," + str(type_id) + ",'" + title + "','" + url + "','" + site + "', now(), now());"
        
        print(sql)
        try:
            #pass
            self.db.execute_not_safe(sql)
        except Exception as e:
            print(str(e))
            pass

    
    def history(self):
        for i in range(1,40):
            url = "https://www.cea.gov.cn/eportal/ui?pageId=363403&currentPage=" + str(i) + "&moduleId=cb58908c969a487cbfc241e2ad6457b8&staticRequest=yes"
            self.realtime(url)
            time.sleep(20)
    
    def realtime(self, url):
        bs4_data = MyHttp.bs4_utf8_data_v2(url)
        #datas = bs4_data.find_all(name="div", attrs={'class':'latestPolicy_left_item'})
        for valid_data in bs4_data.find_all(name="div", attrs={'class':'listNews'}):
            for li in valid_data.find_all(name="li"):
                data_title= li.find("a").text
                if (data_title.find(u"发生6") > 0) or (data_title.find(u"发生7") > 0) or (data_title.find(u"发生8") > 0) or (data_title.find(u"发生9") > 0):
                    data_url= "https://www.cea.gov.cn" + li.find("a").attrs["href"]
                    data_date = li.find("span").text
                    self.insert_data(data_date, data_title, 2150, data_url, "中国地震局")

    
    def get_data(self):
        #self.history()
        self.realtime("https://www.cea.gov.cn/eportal/ui?pageId=363403&currentPage=1&moduleId=cb58908c969a487cbfc241e2ad6457b8&staticRequest=yes")

def dizhen_api():
    d = DizhenData()
    d.get_data()

if __name__ == '__main__':
    dizhen_api()
